<template>
  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iPickBatch-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('reviewApply.fields.batchName')"
        field="batchName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('reviewApply.fields.year')"
        field="year"
        align="center"
        show-header-overflow
        show-overflow
        min-width="50"
      />
      <vxe-column
        :title="t('reviewApply.fields.applyStartTime')"
        field="applyStartTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('reviewApply.fields.applyEndTime')"
        field="applyEndTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('reviewApply.fields.isApply')"
        field="isApply"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      >
        <template #default="{ row }">
          <el-tag v-if="row.isApply" type="success">{{ t('reviewApply.status.apply') }}</el-tag>
          <el-tag v-else type="danger">{{ t('reviewApply.status.unApply') }}</el-tag>
        </template>
      </vxe-column>
      <vxe-column
        :title="t('reviewApply.fields.applyTime')"
        field="applyTime"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
        :formatter="vxeDateFormatter"
      />
      <vxe-column
        :title="t('reviewApply.fields.reviewResult')"
        field="reviewResult"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      >
        <template #default="{ row }">
          <el-tag v-if="row.isApply && !row.reviewResult" type="info">{{
            t('reviewApply.status.unfinished')
          }}</el-tag>
          <el-tag v-else-if="row.isApply && row.reviewResult === 1" type="success">{{
            t('reviewApply.status.passed')
          }}</el-tag>
          <el-tag v-else-if="row.isApply && row.reviewResult === -1" type="danger">{{
            t('reviewApply.status.unPassed')
          }}</el-tag>
        </template>
      </vxe-column>
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="200"
      >
        <template #default="{ row }">
          <el-button
            v-if="!row.isApply"
            link
            type="primary"
            :disabled="Date.now() < row.applyStartTime || Date.now() > row.applyEndTime"
            @click="handleClick(true, row)"
          >
            {{ t('reviewApply.operate.apply') }}
          </el-button>
          <el-button v-if="row.isApply" link type="primary" @click="handleClick(false, row)">
            {{ t('reviewApply.operate.view') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 申请/查看申请信息弹窗 -->
  <ReviewApplyDialog ref="reviewApplyDialogRef" @success="getList" />
</template>

<script setup lang="ts">
import { useAppStore } from '@/store/modules/app'
import { vxeDateFormatter } from '@/utils/formatTime'
import type { VxeTablePropTypes, VxeToolbarInstance, VxeTableInstance } from 'vxe-table'
import { ReviewApplyApi, ReviewApplyVO } from '@/api/recruit/reviewApply'
import ReviewApplyDialog from './components/ReviewApplyDialog.vue'

/** 招生资格审查 申请 */
defineOptions({ name: 'ReviewApply' })

const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()
const reviewApplyDialogRef = ref<InstanceType<typeof ReviewApplyDialog>>()

const loading = ref(true) // 列表的加载中
const list = ref<ReviewApplyVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10
})

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ReviewApplyApi.getReviewBatchList(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

// 点击申请/申请详情
const handleClick = (isEdit: boolean, row: ReviewApplyVO) => {
  reviewApplyDialogRef.value?.open(isEdit, row)
}

/** 初始化 **/
onMounted(() => {
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>
